﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Web;

namespace Beryl.OrderControllers.DataStructural
{
    public class TotalStructural
    {
        private static TotalStructural instance = null;
        private static readonly object uniqueLock = new object();
        public static TotalStructural Instance
        {
            get
            {
                lock (uniqueLock)
                {
                    if (instance == null)
                    {
                        instance = new TotalStructural();
                    }
                }
                return instance;
            }
        }
        public TotalStructural() { }

        /**
 * 生成BOM
 * @param {Array} ManuBOM 数据库bom
 */
        public Dictionary<string, Dictionary<string, Dictionary<string, DataRow>>> getBOM(DataRow[] ManuBOM)
        {
            var BomObj = new Dictionary<string, Dictionary<string, Dictionary<string, DataRow>>>();
            try
            {
                DataTable dt = CommonMethod.Instance.ToDataTable(ManuBOM);
                dt.Columns.Add("UnitUsage", typeof(string));

                DataRow[] dataRows = dt.Select("1 = 1");
                for (int i = 0; i < dataRows.Length; ++i)
                {
                    // 1.母件
                    string MatNo = dataRows[i]["cMatNo"].ToString();
                    // 2.工序编号
                    string OperationNo = dataRows[i]["cOperationNo"].ToString();
                    // 3.指令类型
                    string InOutType = dataRows[i]["cInOutType"].ToString();
                    // 4.子件
                    string ResNo = dataRows[i]["cMatandResNo"].ToString();
                    // 5.子件名称
                    string ResName = dataRows[i]["cMatandResName"].ToString();
                    // 6.子件类型
                    string ResType = dataRows[i]["cMatandResType"].ToString();
                    // 7.用量分子
                    string dQty = dataRows[i]["dQty"].ToString();
                    // 8.用量分母
                    string MeasureUnit = dataRows[i]["dMeasureUnit"].ToString();
                    // 9.规格型号
                    string specifucation = dataRows[i]["cSpecifucation"].ToString();
                    // 10.损耗率
                    string CompScrap = dataRows[i]["CompScrap"].ToString();
                    // 11.最低比容
                    string CapacityLow = dataRows[i]["cCapacityLow"].ToString();
                    // 12.最高比容
                    string Capacityup = dataRows[i]["cCapacityup"].ToString();

                    //dataRows[i]["UnitUsage"] = float.Parse(dQty) / float.Parse(MeasureUnit);
                    if (dQty == "" || MeasureUnit == "")
                    {
                        dataRows[i]["UnitUsage"] = "0";
                    }
                    else
                    {
                        dataRows[i]["UnitUsage"] = float.Parse(dQty) / float.Parse(MeasureUnit);
                    }

                    if (!BomObj.ContainsKey(MatNo))
                    {
                        BomObj.Add(MatNo, new Dictionary<string, Dictionary<string, DataRow>>());
                    }
                    if (!BomObj[MatNo].ContainsKey(InOutType))
                    {
                        BomObj[MatNo].Add(InOutType, new Dictionary<string, DataRow>());
                    }
                    if ("output" == InOutType)
                    {
                        if(!BomObj[MatNo][InOutType].ContainsKey(""))
                        {
                            BomObj[MatNo][InOutType].Add("", null);
                        }
                        BomObj[MatNo][InOutType][""] = dataRows[i];
                    }
                    else if("input" == InOutType)
                    {
                        if (!BomObj[MatNo][InOutType].ContainsKey(ResNo))
                        {
                            BomObj[MatNo][InOutType].Add(ResNo, null);
                        }
                        BomObj[MatNo][InOutType][ResNo] = dataRows[i];
                    }else
                    {
                        throw new Exception(ResNo + "中存在非法的指令类型" + InOutType);
                    }
                }
            }
            catch(Exception e)
            {
                throw e;
            }
            return BomObj;
        }

        /**
 * 生成
 * @param {Array} MatData 数据库
 */
        public Dictionary<string,Dictionary<string,string>> getMatData(DataRow[] MatData)
        {
            var MatDataObj = new Dictionary<string, Dictionary<string, string>>();
            try
            {
                for(int i = 0; i < MatData.Length; i++)
                {
                    // 1.物料编号
                    string cMatNo = MatData[i]["cMatNo"].ToString();
                    // 2.计量单位
                    string cUnit = MatData[i]["cUnit"].ToString();
                    // 3.宽度
                    string dwidth = MatData[i]["dwidth"].ToString();
                    if(!MatDataObj.ContainsKey(cMatNo))
                    {
                        MatDataObj.Add(cMatNo, new Dictionary<string, string>());
                        MatDataObj[cMatNo].Add("cUnit", MatData[i]["cUnit"].ToString());
                        MatDataObj[cMatNo].Add("dwidth", MatData[i]["dwidth"].ToString());
                        MatDataObj[cMatNo].Add("dVoltage", MatData[i]["dVoltage"].ToString());
                        MatDataObj[cMatNo].Add("cSpecifucation", MatData[i]["cSpecifucation"].ToString());
                        MatDataObj[cMatNo].Add("cCapacity", MatData[i]["cCapacity"].ToString());
                        MatDataObj[cMatNo].Add("cMatName", MatData[i]["cMatName"].ToString());
                        MatDataObj[cMatNo].Add("Unit", MatData[i]["Unit"].ToString());
                        MatDataObj[cMatNo].Add("cCateNo", MatData[i]["cCateNo"].ToString());
                    }
                }

            }catch(Exception e)
            {
                throw e;
            }
            return MatDataObj;
        }

        /**
 * 生成BOM
 * @param {Array} APS_Routing 数据库
 */
        public Dictionary<string,Dictionary<string,DataRow>> getRouting(DataRow[] APS_Routing)
        {
            var RoutingObj = new Dictionary<string, Dictionary<string, DataRow>>();
            try
            {
                for (int i = 0; i < APS_Routing.Length; ++i)
                {
                    // 1.物料编号
                    string cMatNo = APS_Routing[i]["cMatNo"].ToString();
                    // 2.顺序号
                    string cOpSeq = APS_Routing[i]["cOpSeq"].ToString();
                    // 3.工序编号
                    string cOperationNo = APS_Routing[i]["cOperationNo"].ToString();
                    // 4.工序名称
                    string cOperationName = APS_Routing[i]["cOperationName"].ToString();
                    if(!RoutingObj.ContainsKey(cMatNo))
                    {
                        RoutingObj.Add(cMatNo, new Dictionary<string, DataRow>());
                    }
                    if (!RoutingObj[cMatNo].ContainsKey(cOpSeq))
                    {
                        RoutingObj[cMatNo].Add(cOpSeq, null);
                    }
                    RoutingObj[cMatNo][cOpSeq] = APS_Routing[i];
                }
            }
            catch (Exception e)
            {
                throw e;
            }
            return RoutingObj;
        }

        public Object[] getSorderMatTotalQty(DataRow[] APS_PlanOrder, Dictionary<string, Dictionary<string, Dictionary<string, DataRow>>> BomObj,
            Dictionary<string, Dictionary<string, string>> MatDataObj, Dictionary<string, Dictionary<string, DataRow>> RoutingObj)
        {
            // 创建一个空的object数组
            object[] array = new object[0];
            var cMatTotalQtyObj = new Dictionary<string, Dictionary<string, List<Dictionary<string, string>>>>();
            var cSONoArr_old = new Dictionary<string, Dictionary<string, string>>();
            var cSONoArr_new = new Dictionary<string, Dictionary<string, string>>();
            try
            {
                // 将新元素添加到数组中,例子
                //array = CommonMethod.Instance.AddElement(array, "Hello"); // 调用自定义函数AddElement()
                //array = CommonMethod.Instance.AddElement(array, 123);    // 再次调用AddElement()
                // 遍历生产订单
                for (int i = 0; i < APS_PlanOrder.Length; ++i)
                {
                    string cPOrderNo = APS_PlanOrder[i]["cPOrderNo"].ToString();    // 生产订单号
                    string cMatNo = APS_PlanOrder[i]["cMatNo"].ToString();  // 生产订单物料编号
                    string dNetQty = APS_PlanOrder[i]["dNetQty"].ToString();    // 钉卷需求
                    string dNetQty1 = APS_PlanOrder[i]["dNetQty1"].ToString();  //加工需求
                    string dNetQty3 = APS_PlanOrder[i]["dNetQty3"].ToString();  //重工需求
                    string cStatusNo = APS_PlanOrder[i]["cStatusNo"].ToString();
                    
                    if (cStatusNo != "0030")
                    {
                        if (!cSONoArr_old.ContainsKey(cPOrderNo))
                        {
                            cSONoArr_old.Add(cPOrderNo, new Dictionary<string, string>());
                            cSONoArr_old[cPOrderNo].Add("cMatNo", null);
                        }
                        // 如果APS_ValidBOM表中已有的该销售订单（已做过物料需求计划）
                        cSONoArr_old[cPOrderNo]["cMatNo"] = cMatNo;
                        // continue;
                    }
                    else
                    {
                        if(!cSONoArr_new.ContainsKey(cPOrderNo))
                        {
                            cSONoArr_new.Add(cPOrderNo, new Dictionary<string, string>());
                            cSONoArr_new[cPOrderNo].Add("cMatNo", null);
                        }
                        cSONoArr_new[cPOrderNo]["cMatNo"] = cMatNo;
                    }
                    if (!cMatTotalQtyObj.ContainsKey(cPOrderNo))
                    {
                        // 物料总需求对象中没有该销售订单
                        cMatTotalQtyObj.Add(cPOrderNo, new Dictionary<string, List<Dictionary<string, string>>>());
                        if (BomObj.ContainsKey(cMatNo))
                        {
                            // BOM中存在成品物料
                            if (BomObj[cMatNo].ContainsKey("output"))
                            {
                                // 存在半成品
                                if (!cMatTotalQtyObj[cPOrderNo].ContainsKey(cMatNo))
                                {
                                    cMatTotalQtyObj[cPOrderNo].Add(cMatNo, new List<Dictionary<string, string>>());
                                }
                                float TotalQty = 0;   //子件总需求
                                var childArr = getSemiProChildProduct(cMatNo, BomObj);  // 半成品子件
                                                                                        // 遍历成品物料（这里childArr.length其实永远为1）
                                string t = cPOrderNo;                                                     // 遍历成品物料中的半成品物料
                                foreach (var ResNo in childArr.Keys)
                                {
                                    foreach (var tmp_proNo in childArr[ResNo].Keys)
                                    {
                                        cMatTotalQtyObj = getChildAll(cMatTotalQtyObj, cPOrderNo, cMatNo, childArr, RoutingObj, MatDataObj, ResNo, tmp_proNo, dNetQty);
                                    }
                                }
                                /*计算加工工序的总需求
                                */
                                // 遍历成品中的半成品
                                foreach (var ResNo in BomObj[cMatNo]["input"].Keys)
                                {
                                    // 遍历工艺路径表中该成品的工艺
                                    foreach (var cOpSeq in RoutingObj[cMatNo].Keys)
                                    {
                                        // 匹配加工工艺到工艺路径表上
                                        string dQty = BomObj[cMatNo]["input"][ResNo]["dQty"].ToString();     // 用量分子
                                        string UnitUsage = BomObj[cMatNo]["input"][ResNo]["UnitUsage"].ToString();
                                        string CompScrap = BomObj[cMatNo]["input"][ResNo]["CompScrap"].ToString();   //子件损耗率
                                                                                                    //string cOpSeq = BomObj[cMatNo]["input"][ResNo].cOperationNo;       // 6. 顺序号
                                        string cOperationNo = RoutingObj[cMatNo][cOpSeq]["cOperationNo"].ToString();     // 4.工序编号
                                        string cOperationName = RoutingObj[cMatNo][cOpSeq]["cOperationName"].ToString();     // 5.工序名称
                                        string cMatandResName = BomObj[cMatNo]["input"][ResNo]["cMatandResName"].ToString();     // 9.物料名称
                                        string cMatandResType = BomObj[cMatNo]["input"][ResNo]["cMatandResType"].ToString();     // 10.物料类型 
                                        string dwidth = MatDataObj[ResNo]["dwidth"].ToString();  // 12.宽度
                                        string cSpecifucation = BomObj[cMatNo]["input"][ResNo]["cSpecifucation"].ToString();     // 14.规则型号
                                        string cUnit = MatDataObj[ResNo]["cUnit"].ToString();    // 17.计量单位
                                        TotalQty = (float)(float.Parse(UnitUsage) * (1 + (float.Parse(CompScrap) * 0.01)) * float.Parse(dNetQty));     // 18.需求量
                                        string cCapacityLow = BomObj[cMatNo]["input"][ResNo]["cCapacityLow"].ToString();     // 19.最低比容
                                        string cCapacityup = BomObj[cMatNo]["input"][ResNo]["cCapacityup"].ToString();   // 20.最高比容
                                        string cCapacity = BomObj[cMatNo]["input"][ResNo]["cCapacity"].ToString();
                                        string cBomid = BomObj[cMatNo]["input"][ResNo]["cBomid"].ToString();
                                        string SortSeq = BomObj[cMatNo]["input"][ResNo]["SortSeq"].ToString();
                                        string cCateNo = MatDataObj[ResNo]["cCateNo"].ToString();
                                        string dVoltage = MatDataObj[ResNo]["dVoltage"].ToString();  // 21.电压
                                        string Unit = MatDataObj[ResNo]["Unit"].ToString();   // 22.生产计量单位
                                        Dictionary<string, string> keys = new Dictionary<string, string>
                                        {
                                            { "cOrderType", "0" },
                                            { "cOperationNo", cOperationNo },
                                            { "cOperationName", cOperationName },
                                            { "cOpSeq", cOpSeq },
                                            { "ResNo", cMatNo },
                                            { "cInOutType", "input" },
                                            { "tmp_proNo", ResNo },
                                            { "cMatandResName", cMatandResName },
                                            { "cMatandResType", cMatandResType },
                                            { "dPlanQty", TotalQty.ToString("0.000000") },
                                            { "dwidth", dwidth },
                                            { "dlength", dQty },
                                            { "cSpecifucation", cSpecifucation },
                                            { "cUnit", cUnit },
                                            { "TotalQty", TotalQty.ToString("0.000000") },
                                            { "cCapacityLow", cCapacityLow },
                                            { "cCapacityup", cCapacityup },
                                            { "cCapacity", cCapacity },
                                            { "cBomid", cBomid },
                                            { "SortSeq", SortSeq },
                                            { "cCateNo", cCateNo },
                                            { "dVoltage", dVoltage },
                                            { "Unit", Unit }
                                        };
                                        cMatTotalQtyObj[cPOrderNo][cMatNo].Add(keys);
                                        break;
                                    }
                                }
                            }else
                            {
                                // 不存在半成品
                                if (!cMatTotalQtyObj[cPOrderNo].ContainsKey(cMatNo))
                                {
                                    cMatTotalQtyObj[cPOrderNo].Add(cMatNo, new List<Dictionary<string, string>>());
                                }
                                // 成品子件
                                var childArr = getProChildProduct(cMatNo, BomObj);
                                foreach (var ResNo in childArr.Keys)
                                {
                                    foreach (var tmp_proNo in childArr[ResNo].Keys)
                                    {
                                        cMatTotalQtyObj = getChildAll(cMatTotalQtyObj, cPOrderNo, cMatNo, childArr, RoutingObj, MatDataObj, ResNo, tmp_proNo, dNetQty);
                                    }
                                }
                            }
                        }
                        else
                        {
                            //logger.warn(`制造BOM中不存在物料：${cMatNo}`);
                        }
                    }else
                    {
                        //logger.warn(`销售订单中有相同子销售订单${cPOrderNo}。`);
                    }
                }
                array = CommonMethod.Instance.AddElement(array, cMatTotalQtyObj);
                array = CommonMethod.Instance.AddElement(array, cSONoArr_old);
                array = CommonMethod.Instance.AddElement(array, cSONoArr_new);
            }
            catch(Exception e)
            {
                throw e;
            }
            return array;
        }

        private Dictionary<string, Dictionary<string, List<Dictionary<string, string>>>> getChildAll(Dictionary<string, Dictionary<string, List<Dictionary<string, string>>>> cMatTotalQtyObj, 
            string cPOrderNo, string cMatNo, Dictionary<string, Dictionary<string, Dictionary<string, string>>> childArr, 
            Dictionary<string, Dictionary<string, DataRow>> RoutingObj, 
            Dictionary<string, Dictionary<string, string>> MatDataObj, string ResNo, string tmp_proNo, string dNetQty)
        {
            Dictionary<string, string> keys = null;
            try
            {
                // 用量分母
                string dMeasureUnit = childArr[ResNo][tmp_proNo]["dMeasureUnit"];
                // 损耗率
                string CompScrap = childArr[ResNo][tmp_proNo]["CompScrap"];

                // 6.顺序号
                string cOpSeq = childArr[ResNo][tmp_proNo]["cOperationNo"];
                string cOperationNo = "";
                string cOperationName = "";
                if (RoutingObj.ContainsKey(ResNo) && RoutingObj[ResNo].ContainsKey(cOpSeq))
                {
                    // 4.工序编号
                    cOperationNo = RoutingObj[ResNo][cOpSeq]["cOperationNo"].ToString();
                    // 5.工序名称
                    cOperationName = RoutingObj[ResNo][cOpSeq]["cOperationName"].ToString();
                }
                // 9.物料名称
                string cMatandResName = childArr[ResNo][tmp_proNo]["cMatandResName"];
                // 10.物料类型
                string cMatandResType = childArr[ResNo][tmp_proNo]["cMatandResType"];
                // 12.宽度
               
            
                string dwidth = MatDataObj[tmp_proNo]["dwidth"];
                // 13.长度
                string dlength = childArr[ResNo][tmp_proNo]["dQty"];
                string UnitUsage = childArr[ResNo][tmp_proNo]["UnitUsage"];
                // 14.规则型号
                string cSpecifucation = childArr[ResNo][tmp_proNo]["cSpecifucation"];
                // 17.计量单位
                string cUnit = MatDataObj[tmp_proNo]["cUnit"];
                // 18.需求量
                string str1 = "正";
                string str2 = "负";
                string str3 = "箔";
                string str4 = "电解纸";
                float TotalQty = 0;

                if ((cMatandResName.IndexOf(str1) != -1 && cMatandResName.IndexOf(str3) != -1) ||
        (cMatandResName.IndexOf(str2) != -1 && cMatandResName.IndexOf(str3) != -1))
                {
                    TotalQty = (float)(float.Parse(UnitUsage) * (1 + float.Parse(CompScrap) * 0.01) * float.Parse(dNetQty) * float.Parse(dwidth) * 0.001);
                    cUnit = "30011";
                }
                else if (cMatandResName.IndexOf(str4) != -1)
                {
                    TotalQty = (float)(float.Parse(UnitUsage) * (1 + float.Parse(CompScrap) * 0.01) * float.Parse(dNetQty));
                    cUnit = "30022";
                }
                else
                {
                    TotalQty = (float)(float.Parse(UnitUsage) * (1 + float.Parse(CompScrap) * 0.01) * float.Parse(dNetQty));
                }

                //TotalQty = (float)Math.Round(TotalQty, 6);//取6位小数
                // 19.最低比容
                string cCapacityLow = childArr[ResNo][tmp_proNo]["cCapacityLow"];
                // 20.最高比容
                string cCapacityup = childArr[ResNo][tmp_proNo]["cCapacityup"];
                string cCapacity = childArr[ResNo][tmp_proNo]["cCapacity"];
                string cBomid = childArr[ResNo][tmp_proNo]["cBomid"];
                string SortSeq = childArr[ResNo][tmp_proNo]["SortSeq"];
                string cCateNo = MatDataObj[tmp_proNo]["cCateNo"];
                // 21.电压
                string dVoltage = MatDataObj[tmp_proNo]["dVoltage"];
                // 22.生产计量单位
                string Unit = MatDataObj[tmp_proNo]["Unit"];

                //更新数组
                keys = new Dictionary<string, string>
                {
                    { "cOrderType", "0" },
                    { "cOperationNo", cOperationNo },
                    { "cOperationName", cOperationName },
                    { "cOpSeq", cOpSeq },
                    { "ResNo", ResNo },
                    { "cInOutType", "input" },
                    { "tmp_proNo", tmp_proNo },
                    { "cMatandResName", cMatandResName },
                    { "cMatandResType", cMatandResType },
                    { "dPlanQty", TotalQty.ToString("0.000000") },
                    { "dwidth", dwidth },
                    { "dlength", dlength },
                    { "cSpecifucation", cSpecifucation },
                    { "cUnit", cUnit },
                    { "TotalQty", TotalQty.ToString("0.000000") },
                    { "cCapacityLow", cCapacityLow },
                    { "cCapacityup", cCapacityup },
                    { "cCapacity", cCapacity },
                    { "cBomid", cBomid },
                    { "cCateNo", cCateNo },
                    { "dVoltage", dVoltage },
                    { "Unit", Unit },
                    { "SortSeq", SortSeq }
                };
                cMatTotalQtyObj[cPOrderNo][cMatNo].Add(keys);
            }
            catch(Exception e)
            {
                throw e;
            }
            return cMatTotalQtyObj;
        }

        private Dictionary<string,Dictionary<string,Dictionary<string,string>>> getSemiProChildProduct(string cMatNo,
            Dictionary<string, Dictionary<string, Dictionary<string, DataRow>>> BomObj)
        {
            var tmpObj = new Dictionary<string, Dictionary<string, Dictionary<string, string>>>();
            try
            {
                foreach (var ResNo in BomObj[cMatNo]["input"].Keys)
                {
                    if(!tmpObj.ContainsKey(ResNo))
                    {
                        tmpObj.Add(ResNo, new Dictionary<string, Dictionary<string, string>>());
                    }
                    // 遍历半成品中的子件的物料编号
                    if (!BomObj.ContainsKey(ResNo))
                    {
                        // logger.warn(`物料${ResNo}没有BOM!`);
                        continue;
                    }
                    foreach (var tmp_proNo in BomObj[ResNo]["input"].Keys)
                    {
                        if (!tmpObj[ResNo].ContainsKey(tmp_proNo))
                        {
                            tmpObj[ResNo].Add(tmp_proNo, new Dictionary<string, string>());
                        }
                        // 2.工序编号
                        string cOperationNo = BomObj[ResNo]["input"][tmp_proNo]["cOperationNo"].ToString();
                        // 5.子件名称
                        string cMatandResName = BomObj[ResNo]["input"][tmp_proNo]["cMatandResName"].ToString();
                        // 6.子件类型
                        string cMatandResType = BomObj[ResNo]["input"][tmp_proNo]["cMatandResType"].ToString();
                        // 7.用量分子
                        string dQty = BomObj[ResNo]["input"][tmp_proNo]["dQty"].ToString();
                        // 8.用量分母
                        string dMeasureUnit = BomObj[ResNo]["input"][tmp_proNo]["dMeasureUnit"].ToString();

                        float UnitUsage = float.Parse(dQty) / float.Parse(dMeasureUnit);
                        //UnitUsage = (float)Math.Round(UnitUsage, 2);//取两位小数

                        // 9.规则型号
                        string cSpecifucation = BomObj[ResNo]["input"][tmp_proNo]["cSpecifucation"].ToString();
                        // 10.损耗率
                        string CompScrap = BomObj[ResNo]["input"][tmp_proNo]["CompScrap"].ToString();
                        // 11.最低比容
                        string cCapacityLow = BomObj[ResNo]["input"][tmp_proNo]["cCapacityLow"].ToString();
                        // 12.最高比容
                        string cCapacityup = BomObj[ResNo]["input"][tmp_proNo]["cCapacityup"].ToString();
                        string cCapacity = BomObj[ResNo]["input"][tmp_proNo]["cCapacity"].ToString();
                        string cBomid = BomObj[ResNo]["input"][tmp_proNo]["cBomid"].ToString();
                        string SortSeq = BomObj[ResNo]["input"][tmp_proNo]["SortSeq"].ToString();

                        tmpObj[ResNo][tmp_proNo].Add("cOperationNo", cOperationNo);
                        tmpObj[ResNo][tmp_proNo].Add("cMatandResName", cMatandResName);
                        tmpObj[ResNo][tmp_proNo].Add("cMatandResType", cMatandResType);
                        tmpObj[ResNo][tmp_proNo].Add("dQty", dQty);
                        tmpObj[ResNo][tmp_proNo].Add("dMeasureUnit", dMeasureUnit);
                        tmpObj[ResNo][tmp_proNo].Add("cSpecifucation", cSpecifucation);
                        tmpObj[ResNo][tmp_proNo].Add("CompScrap", CompScrap);
                        tmpObj[ResNo][tmp_proNo].Add("cCapacityLow", cCapacityLow);
                        tmpObj[ResNo][tmp_proNo].Add("cCapacityup", cCapacityup);
                        tmpObj[ResNo][tmp_proNo].Add("cCapacity", cCapacity);
                        tmpObj[ResNo][tmp_proNo].Add("cBomid", cBomid);
                        tmpObj[ResNo][tmp_proNo].Add("SortSeq", SortSeq);
                        tmpObj[ResNo][tmp_proNo].Add("UnitUsage", UnitUsage.ToString());
                    }
                }
            }
            catch(Exception e)
            {
                throw e;
            }
            return tmpObj;
        }

        /**
* 找出成品(没有半成品)对应的子件
*/
        private Dictionary<string, Dictionary<string, Dictionary<string, string>>> getProChildProduct(string cMatNo, Dictionary<string, Dictionary<string, Dictionary<string, DataRow>>> BomObj)
        {
            var tmpObj = new Dictionary<string, Dictionary<string, Dictionary<string, string>>>();
            try
            {
                if(!tmpObj.ContainsKey(cMatNo))
                {
                    tmpObj.Add(cMatNo, new Dictionary<string, Dictionary<string, string>>());
                }
                // 遍历成品中的子件的物料编号
                foreach (var tmp_proNo in BomObj[cMatNo]["input"].Keys)
                {
                    if (!tmpObj[cMatNo].ContainsKey(tmp_proNo))
                    {
                        tmpObj[cMatNo].Add(tmp_proNo, new Dictionary<string, string>());
                    }
                    // 2.工序编号
                    string cOperationNo = BomObj[cMatNo]["input"][tmp_proNo]["cOperationNo"].ToString();
                    // 5.子件名称
                    string cMatandResName = BomObj[cMatNo]["input"][tmp_proNo]["cMatandResName"].ToString();
                    // 6.子件类型
                    string cMatandResType = BomObj[cMatNo]["input"][tmp_proNo]["cMatandResType"].ToString();
                    // 7.用量分子
                    string dQty = BomObj[cMatNo]["input"][tmp_proNo]["dQty"].ToString();
                    // 8.用量分母
                    string dMeasureUnit = BomObj[cMatNo]["input"][tmp_proNo]["dMeasureUnit"].ToString();
                    float UnitUsage = float.Parse(dQty) / float.Parse(dMeasureUnit);
                    // 9.规则型号
                    string cSpecifucation = BomObj[cMatNo]["input"][tmp_proNo]["cSpecifucation"].ToString();
                    // 10.损耗率
                    string CompScrap = BomObj[cMatNo]["input"][tmp_proNo]["CompScrap"].ToString();
                    // 11.最低比容
                    string cCapacityLow = BomObj[cMatNo]["input"][tmp_proNo]["cCapacityLow"].ToString();
                    // 12.最高比容
                    string cCapacityup = BomObj[cMatNo]["input"][tmp_proNo]["cCapacityup"].ToString();
                    string cCapacity = BomObj[cMatNo]["input"][tmp_proNo]["cCapacity"].ToString();
                    string cBomid = BomObj[cMatNo]["input"][tmp_proNo]["cBomid"].ToString();
                    string SortSeq = BomObj[cMatNo]["input"][tmp_proNo]["SortSeq"].ToString();

                    tmpObj[cMatNo][tmp_proNo].Add("cOperationNo", cOperationNo);
                    tmpObj[cMatNo][tmp_proNo].Add("cMatandResName", cMatandResName);
                    tmpObj[cMatNo][tmp_proNo].Add("cMatandResType", cMatandResType);
                    tmpObj[cMatNo][tmp_proNo].Add("dQty", dQty);
                    tmpObj[cMatNo][tmp_proNo].Add("dMeasureUnit", dMeasureUnit);
                    tmpObj[cMatNo][tmp_proNo].Add("cSpecifucation", cSpecifucation);
                    tmpObj[cMatNo][tmp_proNo].Add("CompScrap", CompScrap);
                    tmpObj[cMatNo][tmp_proNo].Add("cCapacityLow", cCapacityLow);
                    tmpObj[cMatNo][tmp_proNo].Add("cCapacityup", cCapacityup);
                    tmpObj[cMatNo][tmp_proNo].Add("cCapacity", cCapacity);
                    tmpObj[cMatNo][tmp_proNo].Add("cBomid", cBomid);
                    tmpObj[cMatNo][tmp_proNo].Add("SortSeq", SortSeq);
                    tmpObj[cMatNo][tmp_proNo].Add("UnitUsage", UnitUsage.ToString());
                }
            }
            catch (Exception e)
            {
                throw e;
            }
            return tmpObj;
        }
    }
}